//index.js
//获取应用实例 
var app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    token: '',
    accountId: '',
    codeId: '',
    passId: '',
    paddPassId: '',
    inviteId: '',
    inviteCode: '',
    code: '', //验证码
    iscode: null, //用于存放验证码接口里获取到的code
    codename: '获取验证码',
    temp: ''
  },
  onShow: function () {

  },
  onLoad: function () {
    console.log(wx.getStorageSync("auth_code"))
    var auth_code = wx.getStorageSync("auth_code")
    var that = this;
    that.setData({
      inviteId: auth_code
    })
  },
  accountInput: function (e) {
    var that = this;
    var phone = e.detail.value;
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
      this.setData({
        ajxtrue: false
      })
      if (phone.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      this.setData({
        ajxtrue: true
      })
      that.setData({
        accountId: e.detail.value
      })
    }

  },
  codeInput: function (e) {
    var that = this;
    that.setData({
      code: e.detail.value
    })
  },
  passwordInput: function (e) {
    var that = this;
    that.setData({
      passId: e.detail.value
    })
  },
  inviteIdInput: function (e) {
    var that = this;
    that.setData({
      inviteId: e.detail.value
    })
  },
  onReady: function () {

  },
  tologin: function () {
    wx.navigateTo({
      url: 'phone-login',
    })
  },
  //注册
  register: function () {
    var that = this;
    if (that.data.accountId.length == 0) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return;
    }
    if (that.data.code.length == 0) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return;
    }
    if (that.data.passId.length == 0) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return;
    }
    if (that.data.inviteId.length == 0) {
      wx.showToast({
        title: '请输入推荐人手机号或邀请码',
        icon: 'none'
      })
      return;
    }

    console.log(that.data.inviteId)
    var data = {
      openid: app.globalData.openid,
      unionid: app.globalData.openid,
      type: 'wx',
      // nickname: app.globalData.userInfo.nickName,
      // avatar: app.globalData.userInfo.avatarUrl,
      phone: that.data.accountId, //手机号码
      pwd1: that.data.passId, //密码
      pwd2: that.data.passId, //重复密码
      code: that.data.code //手机短信验证码
    }
    if (that.data.inviteId.length > 0) {
      if (that.data.inviteId.length == 11) {
        data.referrer_phone = that.data.inviteId

      } else {
        data.auth_code = that.data.inviteId

      }
    }


    wx.request({
      url: app.globalData.api + '/app.php?c=UserAccount&a=loginOauth',
      data: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          //缓存token
          wx.setStorageSync('token', res.data.data.token); //将userIdEnc存入本地缓存
          wx.setStorageSync('uid', res.data.data.uid); //将userIdEnc存入本地缓存
          app.globalData.token = res.data.data.token
          app.globalData.uid = res.data.data.uid,


            // app.globalData.group_id = res.data.data.group_id
            wx.showToast({
              title: '绑定成功', //提示文字
              duration: 2000, //显示时长
              mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'success', //图标，支持"success"、"loading"  
              success: function () {}, //接口调用成功
              fail: function () {}, //接口调用失败的回调函数  
              complete: function () {} //接口调用结束的回调函数  
            })
          setTimeout(function () {
            //要延时执行的代码
            // wx.redirectTo({
            //   url: 'phone-login',
            // })
            wx.switchTab({
              url: '/pages/live/home',
            })
          }, 1000) //延迟时间 这里是1秒

        } else {
          wx.showToast({
            title: '' + res.data.msg, //提示文字
            duration: 2000, //显示时长
            mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () {}, //接口调用成功
            fail: function () {}, //接口调用失败的回调函数  
            complete: function () {} //接口调用结束的回调函数  
          })
        }
      },
      fail: function () {

      }
    });
  },


  //获取验证码
  getCode: function () {
    var a = this.data.accountId;
    var _this = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.accountId)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      console.log(this.data.accountId)
      wx.request({
        url: app.globalData.api + '/app.php?c=Sms&a=send',
        data: {
          phone: this.data.accountId, //手机号码
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          'platform': '' + app.globalData.platform,
          'version': '' + app.globalData.version,
        },
        method: 'POST',
        dataType: 'json',
        success(res) {
          console.log(res)
          if (res.data.code == 0) {
            _this.setData({
              iscode: res.data.data
            })
            var num = 61;
            var timer = setInterval(function () {
              num--;
              if (num <= 0) {
                clearInterval(timer);
                _this.setData({
                  codename: '重新发送',
                  disabled: false
                })

              } else {
                _this.setData({
                  codename: num + "s"
                })
              }
            }, 1000)

            wx.showToast({
              title: '' + res.data.msg, //提示文字
              duration: 2000, //显示时长
              mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'success', //图标，支持"success"、"loading"  
              success: function () {}, //接口调用成功
              fail: function () {}, //接口调用失败的回调函数  
              complete: function () {} //接口调用结束的回调函数  
            })
          } else {
            wx.showToast({
              title: '' + res.data.msg, //提示文字
              duration: 2000, //显示时长
              mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'none', //图标，支持"success"、"loading"  
              success: function () {}, //接口调用成功
              fail: function () {}, //接口调用失败的回调函数  
              complete: function () {} //接口调用结束的回调函数  
            })
          }
        }
      })

    }
  },
  //获取验证码
  getVerificationCode: function () {
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },
})